﻿Rsd.define('Rsd.Faith.views.MenuTree', {
    extend: 'Rsd.control.Tree',
    xtype: 'f-tree',
    requires: ['Rsd.Faith.stores.MenuStore','Rsd.control.Link'],
    margin: '0 0 0 0',
    width: 200,
    collapsible: true,
    split: true,
    showRoot: false,
    label:{
        xtype:'link',
        height:30,
        space:1,
        width:35,
        visible:true,
        title:'单击隐藏',
        handler:'btn_hide',
        style:{right:'0px'},
        //cls: 'x-sys-header-topic',
        text:'<<'
    },
    constructor: function MenuTree(config) {

        Rsd.apply(this, config);

    },
    /*
    *
    * */
    onAfterRender:function onAfterRender() {
        this.callParent();
        var me= this;
        this.store = Rsd.create('Rsd.Faith.stores.MenuStore'),
            this.store.load(null, function (data) {
                me.addNodes(me.getRoot().id, data);
                me.build();
            });
    },
    /*
    * */
    addNodes: function (pid, menus) {
        var _menu;
        var _node;
        for (var i in menus) {
            _menu = menus[i];
            _node = {id: pid + '__' + i, pid: pid, tag: _menu};
            Rsd.apply(_node, _menu)
            delete  _node.children;
            this.addNode(_node);
            if (_menu.children) {
                this.addNodes(_node.id, _menu.children);
            }
        }

    },
    nodeDblClick: function nodeDblClick(node,event) {

        if (node == null || (node.children && node.children.length > 0)) {
            return;
        }
        var _menu = node.tag;
        var page = node.page;
        if(page && page.isDestroyed()==false)
        {
            Rsd.app.showPage(page);
            return;
        }
        if(_menu.hasOwnProperty('viewType'))
        {
            this.block(function () {
                try
                {
                    page = Rsd.create(_menu.viewType, {menu: _menu,title:_menu.text,selected:true,width:'100%'});
                }
                catch (ex)
                {
                    Rsd.error('Not Found Page:'+_menu.viewType,'Rsd.Faith.views.MenuTree',ex);
                    page = Rsd.create('Rsd.view.SysNotFoundPage', {menu: _menu,title:_menu.text,selected:true});
                }
                node.page = page;
                Rsd.app.showPage(page);
            },2000);

        } else{
            Rsd.alert( '无法打开页面，未找到[viewType]参数（'+_menu.text +'）。' );
        }

    },
    btn_hide:function () {
        Rsd.app.MainForm.hideLeft();
    }
});
